package gob.minedu.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcInsertFileOne {
	
	public static void main(String[] args) {
		String url = "jdbc:mysql://localhost:3306/siscap?autoReconnect=true";
        String user = "root";
        String password = "mysql";
 
        String filePathTaller = "D:/siscap_files/taller.doc";
        String filePathCoordinador = "D:/siscap_files/coordinador.jpg";
        String filePathFormador = "D:/siscap_files/formador.txt";
 
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            
 
            String sql = "INSERT INTO tb_documento (nombre_archivo, tipo_archivo,size_mb, archivo, categoria,created,created_by) values (?, ?, ?, ?,?,now(),1)";
            PreparedStatement statement = conn.prepareStatement(sql);
            
            statement.setString(1, "taller");
            statement.setString(2, ".doc");
            statement.setDouble(3, 1);
            InputStream inputStream = new FileInputStream(new File(filePathTaller));
            statement.setBlob(4, inputStream);
            statement.setString(5, "Material");
 
            int row = statement.executeUpdate();
            if (row > 0) {
                System.out.println("Un archivo insertado Taller.");
            }
            
            statement.setString(1, "coordinador");
            statement.setString(2, ".jpg");
            statement.setDouble(3, 1);
            inputStream = new FileInputStream(new File(filePathCoordinador));
            statement.setBlob(4, inputStream);
            statement.setString(5, "CV");
 
            row = statement.executeUpdate();
            if (row > 0) {
                System.out.println("Un archivo insertado Coordinador.");
            }
            
            statement.setString(1, "formador");
            statement.setString(2, ".txt");
            statement.setDouble(3, 1);
            inputStream = new FileInputStream(new File(filePathFormador));
            statement.setBlob(4, inputStream);
            statement.setString(5, "Informe");
 
            row = statement.executeUpdate();
            if (row > 0) {
                System.out.println("Un archivo insertado Formador.");
            }
            
            
            imprimir("Hola");
            
            
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
		
	}
	
	@Deprecated
	public static void imprimir(String text){
		System.out.println(text);
	}

}
